package com.lsl.service.impl;

import com.lsl.entity.Song;
import com.lsl.entity.Result;
import com.lsl.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MusicServiceImpl implements MusicService {

    @Autowired
    private RestTemplate restTemplate;

    private static final String API_BASE_URL = "https://api.yyy001.com/api/mgmuisc"; // 占位URL

    private String keyword = "";

    @SuppressWarnings("unchecked")
    @Override
    public Result<Song> getSongById(String id) {
        if (keyword.isEmpty()) {
            return Result.error(400, "关键词未设置");
        }
        try {
            String url = API_BASE_URL + "?msg=" + keyword + "&n=" + id + "&br=2&type=json";
            Map<String, Object> response = restTemplate.getForObject(url, Map.class);
            if (response != null && response.get("code").equals(200)) {
                Song song = new Song();
                song.setId(Integer.parseInt(id));
                song.setKeyword(keyword);
                song.setTitle((String) response.get("title"));
                song.setArtist((String) response.get("singer"));
                song.setAlbumImage((String) response.get("cover"));
                song.setSongUrl((String) response.get("music_url"));
                return Result.success(song);
            }
            return Result.error(404, "获取歌曲信息失败");
        } catch (Exception e) {
            return Result.error(500, "获取歌曲信息失败");
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Result<Song> getSongByKeywordAndId(String keyword, String id) {
        try {
            String url = API_BASE_URL + "?msg=" + keyword + "&n=" + id + "&br=2&type=json";
            Map<String, Object> response = restTemplate.getForObject(url, Map.class);
            if (response != null && response.get("code").equals(200)) {
                Song song = new Song();
                song.setId(Integer.parseInt(id));
                song.setKeyword(keyword);
                song.setTitle((String) response.get("title"));
                song.setArtist((String) response.get("singer"));
                song.setAlbumImage((String) response.get("cover"));
                song.setSongUrl((String) response.get("music_url"));
                return Result.success(song);
            }
            return Result.error(404, "获取歌曲信息失败");
        } catch (Exception e) {
            return Result.error(500, "获取歌曲信息失败");
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public Result<List<Song>> searchSongs(String keyword) {
        try {
            this.keyword = keyword;
            String url = API_BASE_URL + "?msg=" + keyword + "&type=json";
            Map<String, Object> response = restTemplate.getForObject(url, Map.class);
            if (response != null && response.get("code").equals(200)) {
                List<Map<String, Object>> data = (List<Map<String, Object>>) response.get("data");
                List<Song> songs = data.stream().map(item -> {
                    Song song = new Song();
                    song.setId((Integer) item.get("n"));
                    song.setTitle((String) item.get("title"));
                    song.setArtist((String) item.get("singer"));
                    return song;
                }).collect(Collectors.toList());
                return Result.success(songs);
            }
            return Result.error(404, "搜索歌曲失败");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return Result.error(500,"搜索歌曲失败");
        }
    }
}
